﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;

namespace TVC_WEB.Global.UserControl
{
    public partial class SubCategory : System.Web.UI.UserControl
    {
        public String MenuCategory = "";
        String sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            MenuCategory = LoadAllMenu();
        }

        private int CountProduct(int iID)
        {
            BUS.CategoriesControl ctctr = new BUS.CategoriesControl();
            BUS.ProductsControl proCtr = new BUS.ProductsControl();
            String cate = Request["cate"].ToString() + "," + iID.ToString().Trim();
            string[] arrTmp = cate.Split(',');


            string arrid = "";
            for (int i = 0; i < arrTmp.Length; i++)
            {
                string tmp = ctctr.GetAllChilds(arrTmp[i], ref sErr);
                if (i == 0)
                    arrid = proCtr.GetIDByArrCate(tmp, ref sErr);
                else arrid = proCtr.GetIDByArrCate(arrid, tmp, ref sErr);
            }

            DataTable dt = proCtr.GetInID(arrid, ref sErr);

            return dt.Rows.Count;
        }

        private string LoadAllMenu()
        {
            String kq = "";

            if (Request["cate"] != null && Request["cate"] != "" && Request["catetype"] != null && Request["catetype"] != "")
            {
                string[] arrTmp = Request["cate"].ToString().Split(',');
                string[] arrType = Request["catetype"].ToString().Split(',');


                CategoriesControl ctr = new CategoriesControl();
                DataTable dt = ctr.get_childs(Convert.ToInt32(arrTmp[0]), ref sErr);
                DataTable dtroot = ctr.GetRootsCategories("", ref sErr);


                //if (dt.Rows.Count > 0)
                //{
                //    kq += "<div>Category";
                //    for (int i = 0; i < dt.Rows.Count; i++)
                //    {
                //        //    CategoriesInfo cate = new CategoriesInfo(dt.Rows[i]);
                //        String urltype = GetURLTYPE(dt.Rows[i]["Type"].ToString(), arrType);
                //        kq += "<div style=\"padding-left:3px\"><a href=\"prolist.aspx?cate=" + Request["cate"].ToString() + "," + dt.Rows[i]["ID"].ToString() + "&catetype=" + urltype + "\">" + dt.Rows[i]["NameVN"].ToString() + "</a></div>";

                //    }
                //    kq += "</div>";
                //}
                kq += "";
                if (dtroot.Rows.Count > 0)
                {
                    for (int i = 0; i < dtroot.Rows.Count; i++)
                    {
                        if (dtroot.Rows[i]["Type"].ToString().Trim() != "source" &&
                            dtroot.Rows[i]["Type"].ToString().Trim() != "standard" &&
                            dtroot.Rows[i]["Type"].ToString().Trim() != "Material")
                        {
                            int temp = CheckExistType(dtroot.Rows[i]["Type"].ToString().Trim(), arrType);
                            if (temp != -1)
                            {
                                DataTable dtt1 = ctr.get_childs(Convert.ToInt32(arrTmp[temp]), ref sErr);
                                if (dtt1.Rows.Count > 0)
                                {
                                    //      kq += "<div class=\"groundsubmenu\" ><div class=\"subcategory\" >" + arrType[temp].ToString() + "</div>";
                                    kq += LoadSubCate(dtt1, arrType, arrTmp, arrType[temp].ToString());
                                    //      kq += "</div><div><p></p></div>";
                                }
                            }
                            else
                            {
                                DataTable dtt2 = ctr.get_childs(Convert.ToInt32(dtroot.Rows[i]["ID"]), ref sErr);
                                if (dtt2.Rows.Count > 0)
                                {
                                    //     kq += "<div class=\"groundsubmenu\" ><div class=\"subcategory\" >" + dtroot.Rows[i]["Description"].ToString() + "</div>";
                                    kq += LoadSubCate(dtt2, arrType, arrTmp, dtroot.Rows[i]["Description"].ToString());
                                    //      kq += "</div><div><p></p></div>";
                                }
                            }
                        }
                    }
                }



                kq += "</div>";
            }

            return kq;
        }

        private String LoadSubCate(DataTable dt, string[] arrType, string[] arrID, string type)
        {
            String kq = "";
            kq += "<div class=\"groundsubmenu\" ><div class=\"subcategory\" >" + type + "</div>";
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //    CategoriesInfo cate = new CategoriesInfo(dt.Rows[i]);
                    String urltype = GetURLTYPE(dt.Rows[i]["ID"].ToString(), arrID, dt.Rows[i]["Type"].ToString(), arrType);
                    int num = CountProduct(Convert.ToInt32(dt.Rows[i]["ID"].ToString()));
                    String numprod = " (" + num + ")";
                    kq += "<div class=\"linefield\" ><a class=\"linkfield\" href=\"prolist.aspx?" + urltype + "\">" + dt.Rows[i]["NameVN"].ToString() + numprod + "</a></div>";

                }
            }
            kq += "</div><div><p></p></div>";
            return kq;
        }

        protected String GetURLTYPE(String id, String[] arrid, String type, String[] arrtype)
        {
            String kq = "";
            String kqid = "";
            int temp = -1;

            for (int i = 0; i < arrtype.Length; i++)
            {
                if (type == arrtype[i].ToString())
                {
                    temp = i;

                }

                kq += arrtype[i] + ",";
            }
            if (temp == -1)
                kq += type;
            else
            {
                if (kq.Length > 0)
                    kq = kq.Remove(kq.Length - 1);
            }
            for (int i = 0; i < arrid.Length; i++)
            {
                if (i == temp)
                    kqid += id + ",";
                else
                    kqid += arrid[i] + ",";
            }
            if (temp == -1)
                kqid += id;
            else
                if (kqid.Length > 0)
                    kqid = kqid.Remove(kqid.Length - 1);
            kqid = "cate=" + kqid.ToString() + "&catetype=" + kq.ToString();
            return kqid;
        }

        protected int CheckExistType(String type, String[] arr)
        {
            int flag = -1;
            for (int i = 0; i < arr.Length; i++)
            {
                if (type == arr[i])
                    return i;
            }
            return flag;
        }
    }
}